package com.drakulo.games.ais.ui.component;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.core.GameData;
import com.drakulo.games.ais.core.tech.Technology;
import com.drakulo.games.ais.ui.component.button.ImageButton;

public class UITechnology extends ImageButton {
	public static final int SIZE = 48;

	/** The technology */
	private Technology technology;
	/** Selection flag */
	private boolean selected;

	public UITechnology(Technology t) {
		this(t.getImageKey());
		setTechnology(t);
	}

	public UITechnology(String imageRef) {
		super(imageRef);
		setWidth(SIZE);
		setHeight(SIZE);

		setActionHandler(new ActionHandler() {

			@Override
			public void run() {
				GameData.setLastViewedTechnology(UITechnology.this.technology);
			}
		});
	}

	public Technology getTechnology() {
		return this.technology;
	}

	public void setTechnology(Technology technology) {
		this.technology = technology;
		this.setTitle(technology.getName());
	}

	public void render(Graphics g) throws SlickException {
		super.render(g);
		if (technology != null) {
			Color c = null;
			if (technology.isOwned()) {
				c = Color.green;
			} else if (technology.isResearchable()) {
				c = Color.yellow;
			} else {
				c = Color.red;
			}
			g.setColor(c);
			g.drawRect(getOX(), getOY(), getWidth(), getHeight());
		}
	}

	public boolean isSelected() {
		return this.selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

}
